// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config";
import global from "../global";
import playerInfoNode from "../ui/playerInfoNode";
import Rank from "../ui/rank";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Leaderboard extends cc.Component {

    //用户信息
    @property(playerInfoNode)
    playerNode: playerInfoNode = null;

    //返回Button
    @property({
        type: cc.Button,
        displayName: "返回",
        tooltip: "返回主页"
    })
    returnHomeButton: cc.Button = null;

    //展示闯关排行Button
    @property({
        type: cc.Button,
        displayName: "闯关排行",
        tooltip: "展示闯关排行"
    })
    breakrankButton: cc.Button = null;

    //展示无尽模式排行Button
    @property({
        type: cc.Button,
        displayName: "竞速排行",
        tooltip: "展示无尽排行"
    })
    speedrankButton: cc.Button = null;

    //返回Button
    @property({
        type: cc.Button,
        displayName: "返回选择",
        tooltip: "返回选择排行"
    })
    returnchooceButton: cc.Button = null;

    //选择展示哪个排行榜的结点
    @property({
        type: cc.Node,
        displayName: "选择排行榜",
        tooltip: "选择展示排行榜"
    })
    chooseleaderboard: cc.Node = null;

    //展示排行榜的结点
    @property({
        type: cc.Node,
        displayName: "排行榜",
        tooltip: "排行榜主体"
    })
    leaderboard: cc.Node = null;

    //排行榜音效资源
    @property({
        type: cc.AudioClip,
        tooltip: "音乐",
        displayName: "背景音乐",
    })
    tapaudio: cc.AudioClip = null;

    //背景渲染
    @property({
        type: cc.Sprite,
        tooltip: "background",
        displayName: "background",
    })
    background: cc.Sprite = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        [this.breakrankButton,this.speedrankButton]=this.chooseleaderboard.getComponentsInChildren(cc.Button);
        this.chooseleaderboard.active=true;//刚开始要显示选择排行榜界面
        this.leaderboard.active=false;//刚开始不展示排行榜
        this.returnchooceButton.node.active=false;//展示排行榜时才展示返回选择的按钮
    }

    start() {
        !!this.playerNode && this.playerNode.loadingInfo();// getInfo(global.player, true);
        this.returnHomeButton && this.returnHomeButton.node.on(cc.Node.EventType.TOUCH_START, () => { this.returnHomeScene() });
        this.breakrankButton && this.breakrankButton.node.on(cc.Node.EventType.TOUCH_START, () => { this.breakrank() });
        this.speedrankButton && this.speedrankButton.node.on(cc.Node.EventType.TOUCH_START, () => { this.speedrank() });
        this.returnchooceButton && this.returnchooceButton.node.on(cc.Node.EventType.TOUCH_START, () => { this.returnchooce() });
    }

    //update(dt) {}
    returnHomeScene() {
        //        if (this.tapaudio) cc.audioEngine.playEffect(this.tapaudio, false);//加载音效资源，尚未载入
        cc.director.loadScene("Home");
    }
    
    //展示闯关排行的按钮的函数
    breakrank() {
        this.chooseleaderboard.active=false;//隐藏选择
        this.returnHomeButton.node.active=false;//返回主页失效
        this.leaderboard.active=true;
        this.returnchooceButton.node.active=true;//展示排行榜时才展示这个
        Rank.getRank();
    }

    //展示无尽模式排行榜的按钮的函数
    speedrank() {
        // console.log("speedrank call getRank2");
        Rank.getRank2();
        this.chooseleaderboard.active=false;//隐藏选择
        this.returnHomeButton.node.active=false;//返回主页失效
        this.leaderboard.active=true;
        this.returnchooceButton.node.active=true;//展示排行榜时才展示这个
    }

    //返回选择哪个排行榜的界面
    returnchooce(){
        this.chooseleaderboard.active=true;//展示选择排行榜界面
        this.returnHomeButton.node.active=true;//可以返回主页
        this.leaderboard.active=false;//隐藏排行榜
        this.returnchooceButton.node.active=false;//展示排行榜时才展示这个
    }
}
